Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
## Chapter 5 Visual Design + Basic Code = Visual Basic By adding a few more fundamentals to what you have learned, you’ll be well on your way to becoming captain of the good ship Visual Basic. We’ve covered a lot of ground in the first four chapters, and I hope you’re beginning to get your Visual Basic sea legs. If I’ve been doing my job well, you’re starting to develop at least a hazy picture of how the various parts of Visual Basic—objects, properties, and code—work together to form a fully functioning application program. To bring this picture into sharper focus, we’re going to tackle a somewhat more ambitious project in this chapter: an on-screen calculator. As you work through this project, you will see how everything I’ve crammed into your head in the past four chapters comes together. Along the way, I’ll present a few more useful Visual Basic tools and techniques. ### Planning The Calculator The first step in planning a calculator is deciding what type of logic it will use. Calculator logic comes in two types: algebraic (the more common) and reverse Polish notation (RPN). With an algebraic calculator, you perform calculations electronically in the same way you would on paper. For example, to add 4 to 7 on paper, you would write:
    4 + 7 = 11
Using an algebraic calculator for this task, you would press 4, press +, press 7, and press =, and the answer would be displayed. In contrast, to perform this calculation using an RPN calculator, you would press 4, press Enter, press 7, then press +. If you are not accustomed to RPN calculators, this probably seems weird to you. It makes a lot of sense, however, and is actually quite similar to internal computer logic. Let’s take a look at how it works. #### RPN And Stacks RPN is based on the concept of a stack, which is a form of data storage using the “last in, first out” method. In other words, when you retrieve a data item from the stack, you’ll get the item that was most recently placed on the stack. If you retrieve another item, you’ll get the one before that, and so on. The most common analogy to a data stack is the way cafeteria plates are often stacked in a spring-loaded tube. As you retrieve plates, they come out in the reverse order in which they were inserted. Here’s what happens, then, when you add 4 and 7 on an RPN calculator: • Press 4— Puts 4 in the display. • Press Enter— Completes entry of the first number. • Press 7— Moves the first entry (4) to the top of the stack and puts 7 in the display. • Press +— Adds the number in the display (7) to the number at the top of the stack (4), displays the result, and removes the top number (4) from the stack. For short calculations, the RPN method really has no advantage over the algebraic method. It’s the longer calculations where it comes into its own. Let’s say you want to add 4 to 7, multiply the result by 2.5, then square that result. Table 5.1 shows how you would enter this calculation, and how it would show in the display. Table 5.1 The steps in an RPN calculation.
Press | Display Reads
--- | ---
4 | 4
Enter | 4
7 | 7
+ | 11
2.5 | 2.5
x | 27.5
2 | 2
Yx | 756.25
On an algebraic calculator, not only would this equation require a lot more keystrokes, but the order of execution would be more difficult to follow. In any case, I’m not here to sell you on the advantages of an RPN calculator, but we will be using RPN logic for our project. If you want a project to try on your own, work on creating an algebraic calculator. You’ll quickly see which type of calculator is easier to program. Before we get started on the calculator, let’s take a closer look at Visual Basic’s Variant data type, as we will be using it in the project. #### The Variant Data Type Most of the data types we examined in the previous chapters have been rather specific—that is, they could hold either string or numeric data, and the numeric types could hold either integer or floating point values. The Variant type is a different animal altogether. A type Variant variable can hold just about any kind of data—string, integer, or floating point. Even more amazing (at least to experienced programmers who are used to the standard fixed data types), the data in a type Variant is automatically treated in the appropriate way. What does this mean? For the most part, it means that the data is treated as either a string or a number, depending on how you are using the variable. Here’s an example. Suppose that MyVariant is a type Variant, and you assign data to it as follows:
    MyVariant = 1234
If you add this variable to a numeric variable, the data will then be treated as a number. Thus, after the following statements execute (assuming X and Y are type Integer )
    X = 111
    Y = X + MyVariant
the variable Y will contain the numeric value 1345. Now, if S is a type String variable, then after executing the statement
    S = “abc” & X & “def”
the variable S will contain the string “abc1234def.” Throughout the book, you will see that the Variant type can be very useful when your program is working with numeric data that must also be treated as a string—formatting data for display in a calculator, for example. A type Variant can also be used to hold an object reference, as we will see in Chapter 7, but this is not relevant to the current project.